package com.ruoyi.inventory.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.ProductCategory;
import com.ruoyi.inventory.service.IProductCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 商品类别Controller
 *
 * @author xwj
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/inventory/category")
@Api(tags = "商品类别")
public class ProductCategoryController extends BaseController {
    @Autowired
    private IProductCategoryService productCategoryService;

    /**
     * 查询商品类别列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody ProductCategory productCategory) {
        startPage();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody ProductCategory productCategory) {
        startOrderBy();
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return success(list);
    }

    /**
     * 导出商品类别列表
     */
    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, ProductCategory productCategory) {
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        ExcelUtil<ProductCategory> util = new ExcelUtil<ProductCategory>(ProductCategory.class);
        util.exportExcel(response, list, "商品类别数据");
    }

    /**
     * 获取商品类别详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(productCategoryService.selectProductCategoryById(id));
    }

    /**
     * 新增商品类别
     */
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody ProductCategory productCategory) {
        SysUser user = getLoginUser().getUser();
        if(productCategory.getId() == null) {
            productCategory.setCreateId(user.getUserId());
            productCategory.setCreateTime(DateUtils.getNowDate());
            productCategory.setCreateBy(user.getNickName());
            productCategoryService.insertProductCategory(productCategory);
        } else {
            productCategory.setUpdateId(user.getUserId());
            productCategory.setUpdateTime(DateUtils.getNowDate());
            productCategory.setUpdateBy(user.getNickName());
            productCategoryService.updateProductCategory(productCategory);
        }

        return success(productCategory);
    }

    /**
     * 修改商品类别
     */
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody ProductCategory productCategory) {
        SysUser user = getLoginUser().getUser();
        productCategory.setUpdateId(user.getUserId());
        productCategory.setUpdateTime(DateUtils.getNowDate());
        productCategory.setUpdateBy(user.getNickName());
        return success(productCategoryService.updateProductCategory(productCategory));
    }

    /**
     * 删除商品类别
     */
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productCategoryService.deleteProductCategoryByIds(ids));
    }

    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody ProductCategory productCategory) {
        int num = productCategoryService.delete(productCategory);
        if(num == -1) {
            return ResponseModel.error("该节点下有子节点，不能直接删除!");
        }
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<ProductCategory>> queryPage(@RequestBody ProductCategory productCategory) {
        Pages<ProductCategory> page = productCategoryService.queryPage(productCategory);
        return ResponseModel.of(page);
    }

    @PostMapping("/batchUpdate")
    @ApiOperation("批量更新")
    public AjaxResult updateTree(@RequestBody List<ProductCategory> list) {
        Date now = new Date();
        Long uid = getUserId();
        String uname = getNickName();

        for(ProductCategory item : list) {
            item.setUpdateBy(uname);
            item.setUpdateId(uid);
            item.setUpdateTime(now);
            productCategoryService.updateProductCategory(item);
        }
        return success();
    }
}
